package com.lagou.request;

import com.lagou.json.Serializer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * @description:
 * @author: 任嘉豪
 * @version: V1.0
 * @date: 20-6-7 下午12:45
 **/
public class RpcDecoder extends ByteToMessageDecoder {

    private Class<?> clazz;

    private Serializer serializer;



    public RpcDecoder(Class<?> clazz, Serializer serializer) {

        this.clazz = clazz;

        this.serializer = serializer;

    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int byteLength = byteBuf.readInt();

        byte[] bytes = new byte[byteLength];
        byteBuf.readBytes(bytes);

        Object deserialize = serializer.deserialize(clazz, bytes);
        list.add(deserialize);
    }
}
